package com.youth.utils;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 用Feign发新请求，原请求的header cookie都默认没有，需要自己设置
 * https://blog.csdn.net/WYA1993/article/details/84304243
 */
@Component
public class FeignCookieInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        HttpServletRequest request = getHttpServletRequest();
        if (null == request) {
            return;
        }

//        if (attrs != null) {
//            HttpServletRequest request = attrs.getRequest();
//            // 如果在Cookie内通过如下方式取
//            Cookie[] cookies = request.getCookies();
//            if (cookies != null && cookies.length > 0) {
//                for (Cookie cookie : cookies) {
//                    requestTemplate.header(cookie.getName(), cookie.getValue());
//                }
//            } else {
//                logger.warn("FeignHeadConfiguration", "获取Cookie失败！");
//            }
//            // 如果放在header内通过如下方式取
//            Enumeration<String> headerNames = request.getHeaderNames();
//            if (headerNames != null) {
//                while (headerNames.hasMoreElements()) {
//                    String name = headerNames.nextElement();
//                    String value = request.getHeader(name);
//                    /**
//                     * 遍历请求头里面的属性字段，将jsessionid添加到新的请求头中转发到下游服务
//                     * */
//                    if ("jsessionid".equalsIgnoreCase(name)) {
//                        logger.debug("添加自定义请求头key:" + name + ",value:" + value);
//                        requestTemplate.header(name, value);
//                    } else {
//                        logger.debug("FeignHeadConfiguration", "非自定义请求头key:" + name + ",value:" + value + "不需要添加!");
//                    }
//                }
//            } else {
//                logger.warn("FeignHeadConfiguration", "获取请求头失败！");
//            }
//        }

        //requestTemplate 好像没有直接设置cookie对象的地方，但cookie其实是header中的
        //Cookie[] cookies = getHttpServletRequest().getCookies();
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                System.out.println("cookie.getName()=" + cookie.getName());
            }
        }
        requestTemplate.header("Cookie", getHttpServletRequest().getHeader("Cookie"));
    }

    /**
     * RequestContextHolder从当前线程中获取请求，又用了线程副本
     *
     * @return
     */
    private HttpServletRequest getHttpServletRequest() {
        try {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        } catch (Exception e) {
            return null;
        }
    }
}
